package cloud.consumer.feign.controller;

import cn.hutool.core.date.DateUtil;
import com.xfanonymous.cloud.api.PayFeignApi;
import com.xfanonymous.cloud.entities.PayDTO;
import com.xfanonymous.cloud.resp.ResultData;
import com.xfanonymous.cloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @auther xfanonymous
 * @create 2023-12-22 22:33
 *
 * OpenFeign
 * TODO: 消费端直接通过Feign接口方法，调用到服务端。替换RestTemplate。
 * 1. POST http://localhost:82/feign/pay/add
 * 2. GET http://localhost:82/feign/pay/get/{{id}}
 * 3. GET http://localhost:82/feign/pay/get/service
 */
@RestController
public class OrderController
{
    @Resource
    private PayFeignApi payFeignApi;

    @PostMapping(value = "/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO)
    {
        System.out.println("第一步：本地addOrder新增订单成功(省略sql操作)，第二步：开启addPay支付微服务远程调用");
        ResultData resultData = payFeignApi.addPay(payDTO);
        return resultData;
    }

    @GetMapping(value = "/feign/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id)
    {
        System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");

        // TODO:验证Feign超时时间
        ResultData resultData = null;
        try
        {
            System.out.println("调用开始-----: "+ DateUtil.now());
            resultData = payFeignApi.getPayInfo(id);
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("调用结束-----: "+ DateUtil.now());
            ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
        return resultData;

//        return payFeignApi.getPayInfo(id);
    }

    /** 操千曲而晓声，观千剑而后识器
     * openfeign天然支持负载均衡
     * @return
     */
    @GetMapping(value = "/feign/pay/get/service")
    public String getServiceInfo()
    {
        return payFeignApi.getServiceInfo();
    }

}